package com.ms.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by RAZANERO on 26/02/14.
 */
public class DateUtil {

    public static SimpleDateFormat FMT_N4() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm", new Locale("en"));
    }

    public static SimpleDateFormat FMT_N4_QRY() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm", new Locale("es"));
    }

    public static SimpleDateFormat FMT_FULL_Z() {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    }

    public static SimpleDateFormat FMT_DAY() {
        return new SimpleDateFormat("yyyy-MM-dd");
    }

    public static SimpleDateFormat FMT_MIN() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }

    public static SimpleDateFormat FMT_WEB() {
        return new SimpleDateFormat("dd/MM/yyyy HH:mm");
    }

    public static SimpleDateFormat FMT_WEB_DAY() {
        return new SimpleDateFormat("dd/MM/yyyy");
    }


    public static SimpleDateFormat FMT_FULL() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static String dateFormat(Date date, SimpleDateFormat simpleDateFormat) {
        if (date == null) return null;
        return simpleDateFormat.format(date);
    }


    public static String dateFormat(Date date, String format) {
        String dateFormat = null;

        if (date != null) {
            DateFormat simpleDateFormat = new SimpleDateFormat(format);
            dateFormat = simpleDateFormat.format(date);
        }
        return dateFormat;
    }

    public static String dateFormat(Date date, Locale locale, String format) throws Exception {
        String dateFormat = null;

        if (date != null) {
            DateFormat simpleDateFormat = new SimpleDateFormat(format, locale);
            dateFormat = simpleDateFormat.format(date);
        }
        return dateFormat;
    }

    public static Date parseDate(String dateStr, String format) throws Exception {
        Date parseDate = null;

        if (dateStr != null) {
            DateFormat simpleDateFormat = new SimpleDateFormat(format);
            parseDate = simpleDateFormat.parse(dateStr);
        }
        return parseDate;
    }
/*

	public static List<LabelValueBean> getYearList(int initialYear, int finalYear) throws Exception {
		List<LabelValueBean> yearList = new ArrayList<LabelValueBean>();

		LabelValueBean labelValueBean = null;
		for (int index = initialYear; index <= finalYear; index++) {
			labelValueBean = new LabelValueBean();
			labelValueBean.setValue(String.valueOf(index));
			labelValueBean.setLabel(String.valueOf(index));

			yearList.add(labelValueBean);
		}
		return yearList;
	}
*/


/*	public static List<LabelValueBean> getMonthList(Locale locale) throws Exception {
		List<LabelValueBean> monthList = new ArrayList<LabelValueBean>();

		LabelValueBean labelValueBean = null;
		Calendar calendar = Calendar.getInstance();

		for (int index = 0; index < 12; index++) {
			calendar.set(Calendar.MONTH, index);

			labelValueBean = new LabelValueBean();
			labelValueBean.setValue(String.valueOf(index));
			labelValueBean.setLabel(dateFormat(calendar.getTime(), locale, "MMMMM"));
			monthList.add(labelValueBean);
		}
		return monthList;
	}*/

    public static String getMonth(Locale locale, int indexMonth) throws Exception {
        String month = null;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, indexMonth);
        month = dateFormat(calendar.getTime(), locale, "MMMMM");
        return month;
    }

    public static Timestamp parseTimestamp(Date date) throws Exception {
        Timestamp parseTimestamp = null;
        parseTimestamp = new Timestamp(date.getTime());
        return parseTimestamp;
    }

    public static Timestamp parseTimestamp(String dateStr, String format) throws Exception {
        Timestamp parseTimestamp = null;
        Date parseDate = parseDate(dateStr, format);
        parseTimestamp = new Timestamp(parseDate.getTime());
        return parseTimestamp;
    }
    public static Date truncDay(Date date) {
        Calendar cal = buildCal(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date createDate(Integer dia, Integer mes, Integer anho) {
        if(dia != null && mes != null && anho != null){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, dia);
            calendar.set(Calendar.MONTH, mes);
            calendar.set(Calendar.YEAR, anho);
            return truncDay(new Date(calendar.getTimeInMillis())) ;
        }
        return null;
    }

    public static Date add(Date date, int field, int timeUnits) {
        Calendar cal =  buildCal(date);
        cal.add(field, timeUnits);
        return cal.getTime();
    }
    private static Calendar buildCal(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    public static Date antDate(Date fecha,int desc){

        Integer dia = getDay(fecha);
        Integer mes = getMes(fecha)+1;
        Integer anho = getAnho(fecha);
        Integer numDias =0;
        numDias = getNumDias(mes-1,anho);

        if(dia>1 && dia <= desc) {
            dia=desc-dia;
            dia=numDias-dia;
            if(mes==1){
                mes=12; anho=anho-1;
            }
            else mes=mes-1;
        }
        else{
            dia=dia-desc;
        }

        Date fechaAnt = newDate(anho,mes,dia);
        Long dif= substractDays(fecha,fechaAnt);

        return fechaAnt;
    }

    public static Integer getAnho(Date fechaDate) {
        if(fechaDate != null){
            GregorianCalendar gcal =  (GregorianCalendar) GregorianCalendar.getInstance();
            gcal.setTime(fechaDate);
            return gcal.get(Calendar.YEAR);
        }
        return null;
    }


    public static Integer getMes(Date fechaDate) {
        if(fechaDate != null){
            GregorianCalendar  gcal = (GregorianCalendar) GregorianCalendar.getInstance();
            gcal.setTime(fechaDate);
            return gcal.get(Calendar.MONTH);
        }
        return null;
    }

    public static Integer  getDay(Date fechaDate) {
        if(fechaDate != null){
            GregorianCalendar  gcal = (GregorianCalendar) GregorianCalendar.getInstance();
            gcal.setTime(fechaDate);
            return gcal.get(Calendar. DAY_OF_MONTH);
        }
        return null;
    }

    public static Long substractDays(Date paidTo, Date paidFrom) {
        if (paidFrom==null||paidTo==null) return null;
        long milis = truncDay(paidTo).getTime() - truncDay(paidFrom).getTime();
        return milis/(1000*60*60*24);

    }

    public static Date newDate(int year, int month, int day) {
        Calendar date = buildCal(new Date());
        date.set(Calendar.YEAR, year);
        date.set(Calendar.MONTH, month-1+Calendar.JANUARY);
        date.set(Calendar.DAY_OF_MONTH, day);

        return truncDay(date.getTime()) ;
    }

    public static Integer getNumDias(Integer mes, Integer anho){
        Integer numDias=0;
        if (mes==0) mes=12;
        switch (mes) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDias = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDias = 30;
                break;
            case 2:
                if ( ((anho % 4 == 0) && !(anho % 100 == 0))
                        || (anho % 400 == 0) )
                    numDias = 29;
                else
                    numDias = 28;
                break;
        }

        return numDias;
    }
}
